<?	/*
	// File:	named.sys.phps
	// Purpose:	named system update
	// Author:	Johan <webcp@100p100.net>
	*/
/* Moved to module bind.mod.phps
///////////////////////
// CONFIGURATION

// filename containing the PTR counter
$filename = "ptrcount.dat";

// Domain Update
if ($type == "domain") {

	// Load current files
	$sysfile['include.named'] = file_get_contents($cfg['basedir']."/named/include/include.named");
	if ($cfg['dns2_system'] == 'webcp')
		$sysfile['include2.named'] = file_get_contents($cfg['basedir']."/web/".$cfg['dns2_file']);
	
	// New files
	$newfile['include.named'] = '# This file is automatically generated by web-cp
	# Please make custom changes with the web panel
	# ================================================
	';
	
	
	if ($cfg['dns2_system'] == 'webcp') {
		$newfile['include2.named'] = '# This file is automatically generated by web-cp
		# Please make custom changes with the web panel
		# ================================================
		';
	}

	// Initialize vars	
	unset($oldowner);
	unset($newfile);
	
	// make include.named file
	$dbp = mysql_query("SELECT DISTINCT(domain) AS domain FROM domains ORDER BY domain");
	while ($data = mysql_fetch_array($dbp))
		$newfile['include.named'] .= "zone \"".$data['domain']."\" {\n\ttype master;\n\tfile \"".$cfg['basedir']."/named/include/".$data['domain'].".named\";\n\tallow-update { ".$cfg['allow-update']."; };\n};\n";
	
	// make include2.named file (dns file secondary ns will fetch)
	if ($cfg['dns2_system'] == 'webcp') {
		$dbp = mysql_query("SELECT DISTINCT(domain) AS domain,ip FROM domains ORDER BY domain");
		while ($data = mysql_fetch_array($dbp)) {
			$newfile['include2.named'] .= "zone \"".$data['domain']."\" {\n\ttype slave;\n\tfile \"".$cfg['dns2_dir']."/".$data['domain'].".named\";\n\tmasters { ".$data['ip']."; };\n};\n\n";
		}
	}
	
	// Make PTR (IP => domain) records
	// use domain.named.tpl as a header for our PTR files, strip whats not needed
	$file = $cfg['basedir']."/named/templates/reverse.named.tpl";
	$namedconf = trim(file_get_contents($file));
	
	// Replace variables by values 
	$namedconf = str_replace('{TTL}', $cfg['dns_ttl'], $namedconf);
	
	// change @ by . in the DNS admin e-mail 
	$tmp = str_replace("@", ".", $cfg['dns_email']);
	$namedconf = str_replace('{ADMMAIL}', $tmp, $namedconf);
	
	// serial (counter)
	if (file_exists($filename)) {
		$tmp = trim(file_get_contents($filename)) + 1;
	} else {
		$tmp = 0;
	}
	file_put_contents($filename,$tmp);
	
	$namedconf = str_replace('{SERIAL}', $tmp, $namedconf);
	$namedconf = str_replace('{REFRESH}', $cfg['dns_refresh'], $namedconf);
	$namedconf = str_replace('{RETRY}', $cfg['dns_retry'], $namedconf);
	$namedconf = str_replace('{EXPIRE}', $cfg['dns_expire'], $namedconf);
	$namedconf = str_replace('{NS1}', $cfg['dns_server1'], $namedconf);
	$namedconf = str_replace('{NS2}', $cfg['dns_server2'], $namedconf);
	$namedconf = str_replace('{NS3}', $cfg['dns_server3'], $namedconf);
	$namedconf = str_replace('{NS4}', $cfg['dns_server4'], $namedconf);
	$namedconf = str_replace('{SERVER}', $cfg['sysname'], $namedconf);
	$namedconf = str_replace('{GPOS}', $cfg['dns_gpos'], $namedconf);
	$namedconf = trim($namedconf)."\n\n";
	
	// get IP list
	$oldptr = '';
	$ptrupdate = false;
	$dbp = mysql_query("SELECT DISTINCT(ip) AS ip,nohost,host,domain FROM domains WHERE priority = 0 ORDER BY ip");
	while ($ptrdata = mysql_fetch_array($dbp)) {
		// Convert IP to an in-addr.arpa according to ip mask
		$tmp = explode(".",$ptrdata['ip']);
		if ($cfg['dns_ipmask'] == 8) {
			$ptrconfig = $tmp[0].".in-addr.arpa";
			$ipleft = $tmp[3].".".$tmp[2].".".$tmp[1];
		} elseif ($cfg['dns_ipmask'] == 16) {
			$ptrconfig = $tmp[1].".".$tmp[0].".in-addr.arpa";
			$ipleft = $tmp[2].".".$tmp[1];
		} elseif ($cfg['dns_ipmask'] == 24) {
			$ptrconfig = $tmp[2].".".$tmp[1].".".$tmp[0].".in-addr.arpa";
			$ipleft = $tmp[3];
		} else {
                        $ptrconfig = $cfg['dns_ipmask'].".".$tmp[2].".".$tmp[1].".".$tmp[0].".in-addr.arpa";
                        $ipleft = $tmp[3];
		}

		if ($ptrdata['nohost'] == 'true')  {
			$ptrline = $ipleft."\tin\tptr\t".$ptrdata['domain'].".";
		} else {
			$ptrline = $ipleft."\tin\tptr\t".$ptrdata['host'].".".$ptrdata['domain'].".";
		}
		
		if ($oldptr == $ptrconfig) {
			$tmpdata .= $ptrline."\n";
		} else {
			if ($oldptr) {
				// Write the PTR named config
				$namedptr = $cfg['basedir']."/named/include/".$oldptr;
				file_put_contents($namedptr,$namedconf.$tmpdata);

				// Set permissions
				chmod($namedptr,0644);
				
				// add a line to the include.named file
				$newfile['include.named'] .= "zone \"$oldptr\" {\n\ttype master;\n\tfile \"".$cfg['basedir']."/named/include/$oldptr\";\n\tallow-update { ".$cfg['allow-update']."; };\n};\n";
			}
			$tmpdata = $ptrline."\n";
		}
			
		$oldptr = $ptrconfig;
	}
	// write the last PTR config
	if ($oldptr) {
		$namedptr = $cfg['basedir']."/named/include/".$oldptr;
		file_put_contents($namedptr,$namedconf.$tmpdata);

		// Set permissions
		chmod($namedptr,0644);
		
		// add a line to the include.named file
		$newfile['include.named'] .= "zone \"$oldptr\" {\n\ttype master;\n\tfile \"".$cfg['basedir']."/named/include/$oldptr\";\n\tallow-update { ".$cfg['allow-update']."; };\n};\n";
	}
	
	
	// Rewrite the include.named if it changed
	if ($newfile['include.named'] != $sysfile['include.named']) {
		file_put_contents($cfg['basedir'].'/named/include/include.named',$newfile['include.named']);
		$restartdns = TRUE;
		
		if ($cfg['dns2_system'] == 'webcp'){
			file_put_contents($cfg['basedir'].'/web/'.$cfg['dns2_file'],$newfile['include2.named']);
		}
	}
}

// end of updatesys("domain");
///
*/
?>
